package com.m7788.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.m7788.screen.R;
public class DialogUploadFile  extends Dialog implements OnItemClickListener{
	
		private Context context;
		OnChooseFileListener onChooseFileListener;
		private ListView listView;
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		@Override
		public void cancel() {
			super.cancel();
		}

		public DialogUploadFile(Context context,OnChooseFileListener opl) {
			super(context,R.style.dialog2);
			this.context = context;
			this.onChooseFileListener = opl;
			this.setContentView(R.layout.dialog_upload_file);
			String json = AppUtil.getRecord(context, Constant.INDEX_MENU);
//			  json="{ret:1,arr:[ {menu:'菜单1',url:'http://183.56.130.114:58080/poe/page/cpd.jsp'}" +
//	  		",{menu:'菜单2',url:'2230'}" +
//	  		",{menu:'菜单3',url:'2231'}" +
//	  		",{menu:'菜单4',url:'2232'}" +
//	  		"]}";
			listView =  (ListView)this.findViewById(R.id.duf_listview) ;
			listView.setOnItemClickListener(this);
			listView.setCacheColorHint(0);
			listView.setDivider(context.getResources().getDrawable(R.drawable.listview_line));
			if(json.length()>0){
				formatJson(json);
			}else{
				
			}
			
			Button btn_left = (Button) findViewById(R.id.duf_btn_left);
			btn_left.setOnClickListener(new View.OnClickListener(){
				@Override
				public void onClick(View arg0) {
					DialogUploadFile.this.cancel();
				}
			});
		}

		
		
		
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Map<String, Object> a = list.get(arg2);
			String url = a.get("url").toString();
			String menu = a.get("menu").toString();
//			Log.e("fileId", fileId);
//			AppUtil.saveRecord(context, Constant.pid, fileId);
			onChooseFileListener.setUploadFile(menu,url);
			this.dismiss();
		}
		
		public interface OnChooseFileListener{
			public void setUploadFile(String menu, String url);
			}
		
		private void formatJson(String json){
			try {
				  String ret = new JSONObject(json).getString("ret");
				  JSONArray array = new JSONObject(json).getJSONArray("arr");
				if("0".equals(ret)){
					
				} else if("1".equals(ret)){
					for(int i=0;i<array.length();i++){
						JSONObject obj = (JSONObject)array.get(i);
						String menu = obj.getString("menu");
						String url = obj.getString("url");
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("menu", menu);
						map.put("url", url);
						map.put("po", i+1+"、");
						list.add(map);
					}
					
					/*
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("menu", "历史记录");
					map.put("url", "sys_999");
					map.put("po", array.length()+1+"、");
					list.add(map);
					
					map = new HashMap<String, Object>();
					map.put("menu", "清除内存");
					map.put("url", "sys_998");
					map.put("po", array.length()+2+"、");
					list.add(map);
					*/
					SimpleAdapter adapter = new SimpleAdapter(context,list,R.layout.adapter_listview_category,new String[]{"menu","po"},new int[]{R.id.alc_tv_attr1,R.id.alc_tv_attr0});
					listView.setAdapter(adapter);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			} 
		}

		
}
